package com.zp.qualitypractice.list.demo;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zp.qualitypractice.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by izp on 2017/5/2.
 */

public class RefreshLoadSwipeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<RefreshSwipeLayoutActivity.SwipeDate> list = new ArrayList<>();

    public RefreshLoadSwipeAdapter(List<RefreshSwipeLayoutActivity.SwipeDate> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType ==1){
            view = View.inflate(parent.getContext(), R.layout.item_swipe_content_with_menu, null);
            return new ContentSwipeMenuHolder(view);
        }else {
            view = View.inflate(parent.getContext(), R.layout.item_swipe_content, null);
            return new SimpleTextHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof SimpleTextHolder) {
            SimpleTextHolder myHolder = (SimpleTextHolder) holder;
            myHolder.item_content.setText(list.get(position).name);
        }else if (holder instanceof ContentSwipeMenuHolder){
            ContentSwipeMenuHolder myHolder = (ContentSwipeMenuHolder) holder;
            myHolder.item_content.setText(list.get(position).name + "######" + position);
            myHolder.item_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), list.get(position).name, Toast.LENGTH_SHORT).show();
                }
            });
            myHolder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    removeData(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).type;
    }

    public void addReFreshData() {
        notifyDataSetChanged();
    }

    public void addRLoadMOreData() {
        notifyDataSetChanged();
    }

    /**
     * @param position
     * 删除一个数据的方法
     *
     * / TODO 一定要按照这个方式写,不然会crash,希望你有更好的解决方案
    // TODO 这么写是为了删除条目的动画,效果,如果不需要动画的,可以进行直接全部刷新数据(notifyDataSetChanged());
     */
    public void removeData(int position){
        notifyItemRemoved(position + 1);//为毛要position+1，难道这里计算了header的，但为毛没有排除footer
        if (position != list.size()) {//如果postion不是footer的位置，position到了size了肯定就是footer了
            if (position == 0) {
                list.remove(position);
                notifyDataSetChanged();
            }else if (position ==list.size()-1){//到了内容item的最后一个
                list.remove(position);//如果list在size之内，不包括，其实都刻意直接remove
                notifyItemRangeChanged(position, 0);//itemCount为0有问题吧，position是开始
            }else {//内容item中间的
                list.remove(position);
                notifyItemRangeChanged(position,list.size()-position+1);//这个是包含了footer的算法吧，删除当前item时
                // ，item到item之后的两个item都变化
            }

        }
    }
    class ContentSwipeMenuHolder extends RecyclerView.ViewHolder {
        private final TextView delete;
        private final TextView ok;
        private final TextView item_content;
        public ContentSwipeMenuHolder(View itemView) {
            super(itemView);
            item_content = (TextView) itemView.findViewById(R.id.item_content_text);
            delete = (TextView) itemView.findViewById(R.id.delete);
            ok = (TextView) itemView.findViewById(R.id.ok);
        }
    }

    class SimpleTextHolder extends RecyclerView.ViewHolder {
        private final TextView item_content;
        public SimpleTextHolder(View itemView) {
            super(itemView);
            item_content = (TextView) itemView.findViewById(R.id.item_content_text);
        }
    }
}
